#!/usr/bin/env node

const { prompt } = require('inquirer');
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const figlet = require('figlet');

const templates = {
  'vue3+H5': 'https://gitee.com/shijia-morning-post_admin/vue3_-h5.git',
  'vue3+layout': 'https://gitee.com/shijia-morning-post_admin/vue3_layout.git',
  'vue3': 'https://gitee.com/shijia-morning-post_admin/vue3.git'
};

function showBanner() {
  console.log(
    chalk.blue(
      figlet.textSync('SJC+VUE3+REACT CLI', {
        font: 'Standard',
        horizontalLayout: 'default',
        verticalLayout: 'default'
      })
    )
  );
  console.log(chalk.cyan('\n🚀 欢迎使用 Vue3+React 项目脚手架！\n'));
}

async function init() {
  showBanner();

  const questions = [
    {
      type: 'list',
      name: 'template',
      message: chalk.green('🎯 请选择项目模板:'),
      choices: [
        {
          name: chalk.yellow('📱 vue3+H5 - 移动端项目模板'),
          value: 'vue3+H5'
        },
        {
          name: chalk.yellow('🖥️  vue3+layout - 管理后台模板'),
          value: 'vue3+layout'
        },
        {
          name: chalk.yellow('⚡ vue3 - 标准 Vue3 模板'),
          value: 'vue3'
        }
      ]
    },
    {
      type: 'input',
      name: 'projectName',
      message: chalk.green('📝 请输入项目名称:'),
      validate: input => {
        if (!input) {
          return chalk.red('项目名称不能为空！');
        }
        return true;
      }
    }
  ];

  const answers = await prompt(questions);
  const { template, projectName } = answers;
  const gitRepo = templates[template];

  console.log(chalk.cyan('\n🚀 开始创建项目...\n'));
  
  try {
    console.log(chalk.yellow('📦 克隆模板...'));
    execSync(`git clone ${gitRepo} ${projectName}`, { stdio: 'inherit' });
    
    console.log(chalk.yellow('🗑️  清理 git 信息...'));
    fs.rmSync(path.join(process.cwd(), projectName, '.git'), { recursive: true, force: true });
    
    console.log(chalk.green('\n✨ 项目创建成功！\n'));
    console.log(chalk.cyan('👉 接下来请执行：\n'));
    console.log(chalk.yellow(`   cd ${projectName}`));
    console.log(chalk.yellow('   npm install'));
    console.log(chalk.yellow('   npm run dev\n'));
  } catch (error) {
    console.error(chalk.red('\n❌ 创建项目失败：'), error);
  }
}

init().catch(error => {
  console.error(chalk.red('\n❌ 发生错误：'), error);
  process.exit(1);
});